@layout('layouts.system')

@section('content')
<div class="page-header">
    <h3>{{ Str::upper(__('admin.staffgroupschedule')->get()) }}</h3>
</div>
<div class="row-fluid">
	<div class="span5 pull-right">
		<a href="#addGroupModal" role="button" class="btn pull-right" data-toggle="modal" style='margin-bottom:10px'><i class="icon-plus"></i>&nbsp;{{ Str::title(__('global.add')->get()) }}</a>
	</div>
</div>
<div id="groupList" class="rows-fluid show-grid">
	{{ $groupList }}
</div>

<!-- Modal -->
<div id="addGroupModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">{{ Str::title(__('admin.addstaffgroup')) }}</h3>
  </div>
  <div class="modal-body">

	{{ Form::open('#', 'POST', array('id' => 'addGroupForm', 'class' => 'form-horizontal')) }}
	{{ Form::hidden('wjkp_no_auto') }}
	{{ Form::control_group(Form::label('wjkp_no_kumpulan', Str::title(__('admin.groupno'))),Form::span2_text('wjkp_no_kumpulan',null,array('required','placeholder'=>Str::title(__('global.type').' '.__('admin.groupno')))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
	{{ Form::control_group(Form::label('wjkp_penerangan', Str::title(__('global.desc'))),Form::span2_text('wjkp_penerangan',null,array('placeholder'=>Str::title(__('global.type').' '.__('global.desc')))));}}
	{{ Form::close()}}

  	
  </div>
  <div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
		<button id="addBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.next')->get()) }}&nbsp;<i class="icon-chevron-right icon-white"></i>&nbsp;</button>
  </div>
</div>

<div id="editGroupModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">{{ Str::title(__('admin.editstaffgroup')) }}</h3>
  </div>
  <div class="modal-body">

	{{ Form::open('#', 'POST', array('id' => 'editGroupForm', 'class' => 'form-horizontal')) }}
	{{ Form::hidden('wjkp_no_auto') }}
	{{ Form::control_group(Form::label('wjkp_no_kumpulan', Str::title(__('admin.groupno'))),Form::span2_text('wjkp_no_kumpulan',null,array('required','placeholder'=>Str::title(__('global.type').' '.__('admin.groupno')))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
	{{ Form::control_group(Form::label('wjkp_penerangan', Str::title(__('global.desc'))),Form::span2_text('wjkp_penerangan',null,array('placeholder'=>Str::title(__('global.type').' '.__('global.desc')))));}}
	{{ Form::close()}}

  	
  </div>
  <div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
		<button id="editBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.edit')->get()) }}</button>
  </div>
</div>
@endsection
@section('scripts')
<script>

	$('#addBtn').click(function() {

		$.post('addGroup', $("#addGroupForm").serialize(),function(data) {
			      sourcedata = data;
			    }).success(function() { 
			    	sourcedata = jQuery.parseJSON(sourcedata);
			    	if(sourcedata.messages){
			    		validated("addGroupForm",sourcedata.messages);
			    	}else{
			    		$("#addGroupForm :input").val('');
			    		$("#groupList" ).empty().html( sourcedata );
						$('#addGroupModal').modal('hide');
			    	}

			    }); 

	});
	
	$('#editBtn').click(function() {

		$.post('editGroupData', $("#editGroupForm").serialize(),function(data) {
			      sourcedata = data;
			    }).success(function() { 
			    	sourcedata = jQuery.parseJSON(sourcedata);
			    	if(sourcedata.messages){
			    		validated("editGroupForm",sourcedata.messages);
			    	}else{				
						$("#groupList" ).empty().html( sourcedata );
			    	}
			    }); 
	});
	
	function editGroupData(id){
		$('#editGroupModal').modal('show');

		$.get("groupInfo",{ id: id}, function(data,status){
			for(x in data){
				$("#editGroupModal .modal-body [name="+x+"]").val(data[x]);
			}
		},"json");
	}

	function editGroup(id){
		$('#addGroupModal').modal('show');

		$.get("groupInfo",{ id: id}, function(data,status){

			for(x in data){

				$("#addGroupModal .modal-body [name="+x+"]").val(data[x]);
			}

		},"json");
	}


	function deleteGroup(id,name){

		var r = confirm("Adakah anda pasti untuk memadam Jadual " +name.toUpperCase() );

		if (r==true){
		    $.post("deleteGroup", "id="+id+"&name="+name ,function(data) {
				      sourcedata = data;
				    }).success(function() {
				    sourcedata = jQuery.parseJSON(sourcedata);

				    if(sourcedata.fail){
				    	alert(sourcedata.fail);
				    }else{
				    	$( "#groupList" ).empty().append( sourcedata );
				    }
					window.location.replace('{{ url("administrator/timetable/groupschedule")}}');
		          });
		}
	}
	
</script>
@endsection